package com.iflytek.day18;

public class _09_SystemDemo {
  public static void main(String[] args) {
    // public static void arraycopy(数据源数组，起始索引，目的地数组，起始索引，拷贝个数) 数组拷贝
    // 细节:
    // 1.如果数据源数组和目的地数组都是基本数据类型，那么两者的类型必须保持一致，否则会报错
    // 2.在拷贝的时候需要考虑数组的长度，如果超出范围也会报错
    // 3.如果数据源数组和目的地数组都是引用数据类型，那么子类类型可以赋值给父类类型

    Student s1 = new Student("zhangsan", 23);
    Student s2 = new Student("lisi", 24);
    Student s3 = new Student("wangwu", 25);

    Student[] arr1 = {s1, s2, s3};
    Person[] arr2 = new Person[3];
    // 把arr1中对象的地址值赋值给arr2中
    System.arraycopy(arr1, 0, arr2, 0, 3);

    // 遍历数组arr2
    for (int i = 0; i < arr2.length; i++) {
      Student stu = (Student) arr2[i];
      System.out.println(stu.getName() + "," + stu.getAge());
    }
  }
}

class Person {
  private String name;
  private int age;

  public Person() {
  }

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  /**
   * 获取
   *
   * @return name
   */
  public String getName() {
    return name;
  }

  /**
   * 设置
   *
   * @param name
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * 获取
   *
   * @return age
   */
  public int getAge() {
    return age;
  }

  /**
   * 设置
   *
   * @param age
   */
  public void setAge(int age) {
    this.age = age;
  }

  public String toString() {
    return "Person{name = " + name + ", age = " + age + "}";
  }
}


class Student extends Person {

  public Student() {
  }

  public Student(String name, int age) {
    super(name, age);
  }
}